package com.code;

import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

public class JsonToJavabean {
	private static Object jsonArray;
	private static ArrayList<EntityType> entityTypes = new ArrayList<>();
	private static List<MonitorItem> monitorItems;
	private static List<PropertyDefinition> commandParams;
	private static List<PropertyDefinition> publicTypeParams;
	private static List<PropertyDefinition> messageParams;


	public void JsonToJavabean(Context context){
		entityTypes = JSON.parseObject(getJson("EntityType.json",  context), new TypeReference<ArrayList<EntityType>>() {});
		monitorItems=JSON.parseObject(getJson("MonitorItem.json",  context), new TypeReference<ArrayList<MonitorItem>>() {});
		commandParams=JSON.parseObject(getJson("CommandParam.json",  context), new TypeReference<ArrayList<PropertyDefinition>>() {});
		publicTypeParams=JSON.parseObject(getJson("PublicTypeParam.json",  context), new TypeReference<ArrayList<PropertyDefinition>>() {});
		messageParams=JSON.parseObject(getJson("MessageParam.json",  context), new TypeReference<ArrayList<PropertyDefinition>>() {});
	}
	public static String getJson(String fileName,Context context) {
		//将json数据变成字符串
		StringBuilder stringBuilder = new StringBuilder();
		try {
			//获取assets资源管理器
			AssetManager assetManager = context.getAssets();
			//通过管理器打开文件并读取
			BufferedReader bf = new BufferedReader(new InputStreamReader(
					assetManager.open(fileName)));
			String line;
			while ((line = bf.readLine()) != null) {
				stringBuilder.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject(stringBuilder.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		try {
			jsonArray= jsonObject.getJSONArray("content");
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return jsonArray.toString();
	}

	public static List<EntityType> getEntityTypes() {
		return entityTypes;
	}

	public static List<MonitorItem> getMonitorItems() {
		return monitorItems;
	}

	public static EntityType entityTypeByCode(int mcode, int tcode) {
		if (monitorItems == null)
			return null;
		for (EntityType entityType : entityTypes) {
			if (entityType.getMcode() == mcode && entityType.getCode() == tcode)
				return entityType;
		}
		return null;
	}

	public static MonitorItem monitorItemByCode(int pcode) {
		if (monitorItems == null)
			return null;
		for (MonitorItem monitorItem : monitorItems) {
			if (monitorItem.getCode() == pcode)
				return monitorItem;
		}
		return null;
	}

	public static PropertyDefinition commandParamByCode(int pcode) {
		if (commandParams == null)
			return null;
		for (PropertyDefinition commandParam : commandParams) {
			if (commandParam.getCode() == pcode)
				return commandParam;
		}
		return null;
	}

	public static PropertyDefinition publicTypeParamByCode(int pcode) {
		if (publicTypeParams == null)
			return null;
		for (PropertyDefinition publicTypeParam : publicTypeParams) {
			if (publicTypeParam.getCode() == pcode)
				return publicTypeParam;
		}
		return null;
	}

	public static PropertyDefinition messageParamByCode(int pcode) {
		if (messageParams == null)
			return null;
		for (PropertyDefinition messageParam : messageParams) {
			if (messageParam.getCode() == pcode)
				return messageParam;
		}
		return null;
	}
	public static  PropertyDefinition getPropertyDefinition(EntityType entityType, int pcode) {
		List<PropertyDefinition> propertyDefinitions = entityType.getPropertyDefinitions();
		for (PropertyDefinition propertyDefinition : propertyDefinitions) {
			if (propertyDefinition.getCode() == pcode) {
				return propertyDefinition;
			}
		}
		return null;

	}


}